E-Tech Timer / E-Tech Timer / stopwatch.vbon commit add archived files (d846247)
   1'All code written by Andrew Lorimer for E-Tech Software
   2
   3Imports System.Text.RegularExpressions
   4Public Class stopwatch
   5
   6    'DECLERATION OF VARIABLES
   7    Public startTime As System.DateTime 'Stores when the timer was started
   8    Public elapsedTime As System.TimeSpan 'Stores the elapsed time
   9    Public hasBeenPaused As Boolean = False 'Determines whether the time has been paused
  10    Public startPause As System.DateTime 'Determines when the user pressed pause
  11    Public elapsedPause As System.TimeSpan 'How long the current session of paused time has been going for
  12    Public elapsedPauseAddends As System.TimeSpan 'How long other sessions of paused time have been going for
  13    Public hasAlerted As Boolean = False 'Determines whether the timer has alerted the user
  14    Public firstStart As Boolean = False 'Is this the first form?
  15    Public alertTime As TimeSpan = TimeSpan.Parse("00:05:00.0") 'Time to alert the user
  16
  17#Region "UI Events"
  18
  19    Private Sub KryptonButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton2.Click
  20        reset()
  21    End Sub
  22
  23    Private Sub KryptonButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton4.Click
  24        'Show/hide the log
  25        If Me.Size.Width = 650 Then
  26            Me.MinimumSize = New Size(0, 0)
  27            Me.MaximumSize = New Size(0, 0)
  28            Me.Width = 397
  29            Me.MinimumSize = New Size(397, 286)
  30            Me.MaximumSize = New Size(397, 286)
  31            KryptonButton4.Text = "Show Log"
  32        Else
  33            Me.MinimumSize = New Size(0, 0)
  34            Me.MaximumSize = New Size(0, 0)
  35            Me.Width = 650
  36            Me.MinimumSize = New Size(650, 286)
  37            Me.MaximumSize = New Size(650, 286)
  38            KryptonButton4.Text = "Hide Log"
  39            KryptonListBox1.Focus()
  40        End If
  41    End Sub
  42
  43    Private Sub KryptonButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton5.Click
  44        'Add a logged time
  45        My.Settings.logID = My.Settings.logID + 1
  46        KryptonListBox1.Items.Add("Log " & My.Settings.logID.ToString & " | " & Label1.Text)
  47        Me.MinimumSize = New Size(0, 0)
  48        Me.MaximumSize = New Size(0, 0)
  49        Me.Width = 650
  50        Me.MinimumSize = New Size(650, 286)
  51        Me.MaximumSize = New Size(650, 286)
  52        KryptonButton4.Text = "Hide Log"
  53        KryptonListBox1.Focus()
  54    End Sub
  55
  56    Private Sub timer_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
  57        'Save the log
  58        My.Settings.logStopwatch.Clear()
  59        For Each item In KryptonListBox1.Items
  60            My.Settings.logStopwatch.Add(item)
  61        Next
  62    End Sub
  63
  64    Private Sub timer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  65        'Starts timing if nessecary
  66        If My.Settings.autoTime Then
  67            start(TimeSpan.Parse("00:00:00.0"))
  68        End If
  69    End Sub
  70
  71    Private Sub KryptonButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton6.Click
  72        'Starts at the selected time
  73        If Not KryptonListBox1.SelectedItem = "" Then
  74            Dim strArr() As String
  75            strArr = KryptonListBox1.SelectedItem.Split("| ")
  76
  77            reset()
  78            start(TimeSpan.Parse(strArr(1).ToString))
  79        End If
  80    End Sub
  81
  82    Private Sub KryptonButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton7.Click
  83        'Remove the selected logged time
  84        Dim lst As New List(Of Object)
  85        For Each a As Object In KryptonListBox1.SelectedItems
  86            lst.Add(a)
  87        Next
  88        For Each a As Object In lst
  89            KryptonListBox1.Items.Remove(a)
  90        Next
  91    End Sub
  92
  93    Private Sub KryptonButton8_Click(sender As Object, e As EventArgs) Handles KryptonButton8.Click
  94        'Shows the settings dialog
  95        settings.Show()
  96        settings.BringToFront()
  97    End Sub
  98
  99    Private Sub NotifyIcon1_Click(sender As Object, e As EventArgs) Handles NotifyIcon1.Click
 100        'Focus the window when the Notify Icon is clicked
 101        Me.Focus()
 102    End Sub
 103
 104    Private Sub KryptonButton10_Click(sender As Object, e As EventArgs) Handles KryptonButton10.Click
 105        'Shows the countdown window and closes the stopwatch window
 106        countdown.Show()
 107        countdown.Location = New Point(Me.Location.X, Me.Location.Y)
 108        countdown.Focus()
 109        My.Settings.Save()
 110        Me.Close()
 111    End Sub
 112
 113    Private Sub KryptonTextBox1_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox1.TextChanged
 114        'Synchronises the label text and form text with the text in the textbox
 115        Label1.Text = KryptonTextBox1.Text
 116        Label1.Left = (198.5) - (Label1.Width / 2)
 117        Me.Text = KryptonTextBox1.Text
 118    End Sub
 119
 120    Private Sub stopwatch_Shown(sender As Object, e As EventArgs) Handles Me.Shown
 121        'Check if this is the right form
 122        If My.Settings.startupMode = "c" And My.Settings.firstStart = False Then
 123            countdown.Show()
 124            countdown.Location = New Point(Me.Location.X, Me.Location.Y)
 125            countdown.Focus()
 126            Me.Close()
 127        End If
 128        My.Settings.firstStart = True
 129
 130        'Loads the log and other values
 131        For Each item In My.Settings.logStopwatch
 132            KryptonListBox1.Items.Add(item)
 133        Next
 134
 135        If My.Settings.hideExtra = True Then
 136            Label1.Text = "00:00:00.0"
 137            Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form
 138            Me.Text = Label1.Text 'Update form text
 139        Else
 140            Label1.Text = "00:00:00.00000"
 141            Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form
 142            Me.Text = Label1.Text 'Update form text
 143        End If
 144
 145        If My.Settings.updateCheck = False Then
 146            BackgroundWorker1.RunWorkerAsync()
 147            My.Settings.updateCheck = True
 148        End If
 149
 150        If My.Settings.showLogDefault = True Then
 151            Me.MinimumSize = New Size(0, 0)
 152            Me.MaximumSize = New Size(0, 0)
 153            Me.Width = 650
 154            Me.MinimumSize = New Size(650, 286)
 155            Me.MaximumSize = New Size(650, 286)
 156            KryptonButton4.Text = "Hide Log"
 157        End If
 158        Me.StartPosition = FormStartPosition.Manual
 159    End Sub
 160
 161    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
 162        'Checks for an updated version
 163        Try
 164            Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://members.optusnet.com.au/filtered/timerVersion.txt")
 165            Dim response As System.Net.HttpWebResponse = request.GetResponse()
 166            Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
 167            Dim version As String = sr.ReadToEnd
 168            If version > "5.0" Then
 169                Dim result As DialogResult = MessageBox.Show("Version " & version & " is now available. Press OK to download the update.", "New Version Available", MessageBoxButtons.OKCancel)
 170                If result = Windows.Forms.DialogResult.OK Then
 171                    Process.Start("http://etechtimer.codeplex.com/releases/")
 172                End If
 173            End If
 174        Catch ex As Exception
 175        End Try
 176    End Sub
 177
 178    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
 179        'Copy current time to clipboard
 180        Clipboard.SetText(Label1.Text)
 181        NotifyIcon1.ShowBalloonTip(1000, "Copied to clipboard", "The elapsed time has been copied to your clipboard.", ToolTipIcon.Info)
 182    End Sub
 183
 184    Private Sub KryptonListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles KryptonListBox1.KeyDown
 185        'Delete selected item with DELETE key
 186        If e.KeyCode = Keys.Delete Then
 187            'Remove the selected logged time
 188            Dim lst As New List(Of Object)
 189            For Each a As Object In KryptonListBox1.SelectedItems
 190                lst.Add(a)
 191            Next
 192            For Each a As Object In lst
 193                KryptonListBox1.Items.Remove(a)
 194            Next
 195        End If
 196        If e.Control And e.KeyCode.ToString = "A" Then
 197            'Select all logged items
 198            For i As Integer = 0 To KryptonListBox1.Items.Count - 1
 199                KryptonListBox1.SetSelected(i, True)
 200            Next
 201        End If
 202    End Sub
 203
 204    Private Sub KryptonListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles KryptonListBox1.SelectedIndexChanged
 205        'Disables/enables "Start at Selected" button based on selection
 206        If KryptonListBox1.SelectedItems.Count > 1 Then
 207            KryptonButton6.Enabled = False
 208        Else
 209            KryptonButton6.Enabled = True
 210        End If
 211    End Sub
 212
 213    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
 214        'Bring the form to the front of screen
 215        Me.WindowState = FormWindowState.Normal
 216    End Sub
 217
 218    Private Sub StartTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartTimingToolStripMenuItem.Click
 219        'Start timing
 220        start(TimeSpan.Parse("00:00:00.0"))
 221    End Sub
 222
 223    Private Sub PauseTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PauseTimingToolStripMenuItem.Click
 224        'Pause timing
 225        pause()
 226        Me.WindowState = FormWindowState.Normal
 227    End Sub
 228
 229    Private Sub ResetTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetTimingToolStripMenuItem.Click
 230        'Reset timing
 231        reset()
 232
 233        'Focus start button and form
 234        KryptonButton9.Focus()
 235        KryptonButton9.Text = "Start"
 236        Me.WindowState = FormWindowState.Normal
 237    End Sub
 238
 239    Private Sub LogTimeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogTimeToolStripMenuItem.Click
 240        'Add a logged time
 241        KryptonListBox1.Items.Add(Label1.Text)
 242        Me.Width = 650
 243        KryptonButton4.Text = "Hide Log"
 244        Me.WindowState = FormWindowState.Normal
 245    End Sub
 246
 247    Private Sub RenameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RenameToolStripMenuItem.Click
 248        'Rename a logged item
 249        If Not KryptonListBox1.SelectedItem = "" Then
 250            RenameStopwatch.Show()
 251        End If
 252    End Sub
 253
 254    Private Sub StartAtSelectedToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartAtSelectedToolStripMenuItem.Click
 255        'Starts at the selected time
 256        If Not KryptonListBox1.SelectedItem = "" Then
 257            Dim strArr() As String
 258            strArr = KryptonListBox1.SelectedItem.Split("| ")
 259            reset()
 260            start(TimeSpan.Parse(strArr(1).ToString))
 261        End If
 262    End Sub
 263
 264    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
 265        'Remove the selected logged time
 266        If Not KryptonListBox1.SelectedItem = "" Then
 267            Dim lst As New List(Of Object)
 268            For Each a As Object In KryptonListBox1.SelectedItems
 269                lst.Add(a)
 270            Next
 271            For Each a As Object In lst
 272                KryptonListBox1.Items.Remove(a)
 273            Next
 274        End If
 275    End Sub
 276
 277    Private Sub KryptonButton9_Click_1(sender As Object, e As EventArgs) Handles KryptonButton9.Click
 278
 279        If KryptonButton9.Text = "Start" Or KryptonButton9.Text = "Resume" Then
 280
 281            'Start at:
 282            Dim matches As MatchCollection = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox1.Text)
 283            Dim output As String
 284            Try
 285                For Each value As Integer In {2, 4, 5} ' Index of current working value
 286                    Dim val = matches(0).Groups(value).Value ' Current working value
 287                    If Not value = 5 Then ' Format hours, minutes
 288                        If val = "" Then
 289                            val = "00"
 290                        End If
 291                        val = val + ":"
 292                    Else ' Format seconds
 293                        If val = "" Then
 294                            val = "00.0"
 295                        End If
 296                    End If
 297                    output = output + val
 298                Next
 299
 300                'Alert at:
 301                matches = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox2.Text)
 302                Dim output2 As String
 303                For Each value As Integer In {2, 4, 5} ' Index of current working value
 304                    Dim val = matches(0).Groups(value).Value ' Current working value
 305                    If Not value = 5 Then ' Format hours, minutes
 306                        If val = "" Then
 307                            val = "00"
 308                        End If
 309                        val = val + ":"
 310                    Else ' Format seconds
 311                        If val = "" Then
 312                            val = "00.0"
 313                        End If
 314                    End If
 315                    output2 = output2 + val
 316                Next
 317
 318                alertTime = TimeSpan.Parse(output2)
 319                start(TimeSpan.Parse(output))
 320            Catch ex As Exception
 321                Dim result As DialogResult = MessageBox.Show("Check that 'Start At' and 'Alert At' are in acceptable formats." & Microsoft.VisualBasic.vbNewLine & "Press OK to see acceptable formats.", "Invalid Format", MessageBoxButtons.OKCancel)
 322
 323                If result = Windows.Forms.DialogResult.OK Then
 324                    Process.Start("https://etechtimer.codeplex.com/wikipage?title=Acceptable%20Input%20Formats")
 325                End If
 326
 327            End Try
 328        Else
 329            pause()
 330        End If
 331    End Sub
 332
 333#End Region
 334
 335
 336
 337
 338#Region "Timer Functionality"
 339    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 340        Try
 341            'Happens every 1ms...
 342
 343            'Update elapsed time:
 344            elapsedTime = (DateTime.Now - startTime) - elapsedPause
 345            If My.Settings.hideExtra = True Then
 346                Label1.Text = elapsedTime.ToString.Substring(0, elapsedTime.ToString.IndexOf(".") + 2) 'Hide extra characters
 347                Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form
 348                Me.Text = Label1.Text 'Update form text
 349                NotifyIcon1.Text = elapsedTime.ToString.Substring(0, elapsedTime.ToString.IndexOf(".") + 2) & " - E-Tech Timer" 'Update systray icon
 350            Else
 351                Label1.Text = elapsedTime.ToString
 352                Label1.Left = (198.5) - (Label1.Width / 2) 'Center label in form
 353                Me.Text = Label1.Text 'Update form text
 354                NotifyIcon1.Text = elapsedTime.ToString & " - E-Tech Timer" 'Update systray icon
 355            End If
 356
 357            'Check if the program should alert the user:
 358            If KryptonCheckBox1.Checked And elapsedTime > alertTime And hasAlerted = False Then
 359
 360                If My.Settings.keepCounting = False Then 'Check if the timer should keep going...
 361                    Timer1.Stop() 'If not, stop the clock
 362                    KryptonButton9.Text = "Start"
 363                End If
 364
 365                'Play and show the alert
 366                Dim Sound As New System.Media.SoundPlayer()
 367                If My.Settings.alertName = "Horn" Then
 368                    Sound.Stream = My.Resources.horn
 369                ElseIf My.Settings.alertName = "Morning" Then
 370                    Sound.Stream = My.Resources.morning
 371                ElseIf My.Settings.alertName = "Tinkle" Then
 372                    Sound.Stream = My.Resources.tinkle
 373                End If
 374                Sound.Load()
 375                Sound.Play()
 376                NotifyIcon1.ShowBalloonTip(1000, "Time's up! - E-Tech Timer", "The specified time has been reached. Click here to open E-Tech Timer.", ToolTipIcon.Info)
 377
 378                hasAlerted = True
 379            End If
 380
 381            hasBeenPaused = False 'Makes sure that this is false
 382        Catch ex As Exception
 383
 384        End Try
 385    End Sub
 386
 387    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
 388        'Happens every 1ms WHEN THE TIMER IS PAUSED
 389        elapsedPause = (DateTime.Now - startPause) + elapsedPauseAddends
 390    End Sub
 391
 392    Sub start(ByVal inputTime As TimeSpan)
 393        'Start timing
 394        Timer2.Stop()
 395        If hasBeenPaused = False Then
 396            startTime = System.DateTime.Now - inputTime
 397            Try
 398                If inputTime > alertTime Then
 399                    KryptonCheckBox1.Checked = False
 400                End If
 401            Catch
 402            End Try
 403        Else
 404            elapsedPauseAddends = elapsedPause
 405        End If
 406        Timer1.Start()
 407        KryptonButton9.Text = "Pause"
 408    End Sub
 409
 410    Sub pause()
 411        'Pause timing
 412        If Timer1.Enabled = True Then
 413            Timer1.Stop()
 414            startPause = DateTime.Now
 415            Timer2.Start()
 416            hasBeenPaused = True
 417        End If
 418        KryptonButton9.Text = "Resume"
 419    End Sub
 420
 421    Sub reset()
 422        'Reset timing
 423
 424        'Stop the timers:
 425        Timer1.Stop()
 426        Timer2.Stop()
 427
 428        'Reset values:
 429        elapsedTime = Nothing
 430        elapsedPause = Nothing
 431        elapsedPauseAddends = Nothing
 432        If My.Settings.hideExtra = True Then
 433            Label1.Text = KryptonTextBox1.Text
 434            Label1.Left = 198.5 - (Label1.Width / 2) 'Center label in form
 435            Me.Text = Label1.Text 'Update form text
 436        Else
 437            Label1.Text = KryptonTextBox1.Text
 438            Label1.Left = 198.5 - (Label1.Width / 2) 'Center label in form
 439            Me.Text = Label1.Text 'Update form text
 440        End If
 441        hasBeenPaused = False
 442        Label1.Left = 198.5 - (Label1.Width / 2)
 443        hasAlerted = False
 444
 445        'Focus start button
 446        KryptonButton9.Focus()
 447        KryptonButton9.Text = "Start"
 448    End Sub
 449#End Region
 450
 451    Private Sub KryptonTextBox2_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox2.TextChanged
 452            'Interpret input
 453            Dim matches As MatchCollection = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox2.Text)
 454            Dim output As String
 455            Try
 456                For Each value As Integer In {2, 4, 5} ' Index of current working value
 457                    Dim val = matches(0).Groups(value).Value ' Current working value
 458                    If Not value = 5 Then ' Format hours, minutes
 459                        If val = "" Then
 460                            val = "00"
 461                        End If
 462                        val = val + ":"
 463                    Else ' Format seconds
 464                        If val = "" Then
 465                            val = "00.0"
 466                        End If
 467                    End If
 468                    output = output + val
 469            Next
 470            If Not Timer1.Enabled And Not TimeSpan.Parse(output) = TimeSpan.Zero Then
 471                alertTime = TimeSpan.Parse(output)
 472            End If
 473
 474            Catch ex As Exception
 475            End Try
 476    End Sub
 477
 478    Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs)
 479        Me.Focus()
 480    End Sub
 481
 482
 483End Class